package com.okwanshiwu.payment.wxUtil;

import java.io.Serializable;
import java.util.Map;
import java.util.TreeMap;

/**
 * 微信订单查询实体类
 * @author master
 * */
public class QueryOrder implements Serializable {

	private static final long serialVersionUID = -6392272697398973454L;
	
	private String appid;
	private String mch_id;
	private String transaction_id;
	private String out_trade_no;
	private String nonce_str;
	private String sign;

	public QueryOrder(String appid,String mchid,String transaction_id,String out_trade_no,String nonce_str){
		this.appid = appid;
		this.mch_id = mchid;
		this.transaction_id = transaction_id;
		this.out_trade_no = out_trade_no;
		this.nonce_str = nonce_str;
		Map<String,String> parameters = new TreeMap<String,String>();
		parameters.put("appid", appid);
		parameters.put("mch_id", mch_id);
		parameters.put("nonce_str", nonce_str);
		if(this.transaction_id == null || "".equals(this.transaction_id)){
			parameters.put("out_trade_no", this.out_trade_no);
		}else{
			parameters.put("transaction_id", this.transaction_id);
		}
		this.sign = WxPayUtil.createSign(parameters);
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getMch_id() {
		return mch_id;
	}

	public void setMch_id(String mch_id) {
		this.mch_id = mch_id;
	}

	public String getTransaction_id() {
		return transaction_id;
	}

	public void setTransaction_id(String transaction_id) {
		this.transaction_id = transaction_id;
	}

	public String getOut_trade_no() {
		return out_trade_no;
	}

	public void setOut_trade_no(String out_trade_no) {
		this.out_trade_no = out_trade_no;
	}

	public String getNonce_str() {
		return nonce_str;
	}

	public void setNonce_str(String nonce_str) {
		this.nonce_str = nonce_str;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String toRequestXml(){
		StringBuffer bf = new StringBuffer();
		bf.append("<xml>");
		bf.append("<appid><![CDATA[");
		bf.append(this.appid);
		bf.append("]]></appid>");
		
		bf.append("<mch_id><![CDATA[");
		bf.append(this.mch_id);
		bf.append("]]></mch_id>");
		
		bf.append("<nonce_str><![CDATA[");
		bf.append(this.nonce_str);
		bf.append("]]></nonce_str>");
		
		if(this.transaction_id != null && !"".equals(this.transaction_id)){
			bf.append("<transaction_id><![CDATA[");
			bf.append(this.transaction_id);
			bf.append("]]></transaction_id>");
		}else{
			bf.append("<out_trade_no><![CDATA[");
			bf.append(this.out_trade_no);
			bf.append("]]></out_trade_no>");
		}
		
		bf.append("<sign><![CDATA[");
		bf.append(this.sign);
		bf.append("]]></sign>");
		bf.append("</xml>");
		return bf.toString();
	}
	
}
